package cn.edu.zafu.bigdata.hzq;
import java.io.Serializable;
import java.io.*;
import java.util.*;
public class Student{
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}



 class StudentManagementSystem {
    private static final String FILENAME = "students.ser";
    private static List<Student> students = new ArrayList<>();

    public static void main(String[] args) {
        loadStudents();
        boolean running = true;
        Scanner scanner = new Scanner(System.in);

        while (running) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    findStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    running = false;
                    break;
                default:
                    System.out.println("无效的选项，请重新输入。");
                    break;
            }
        }

        saveStudents();
        scanner.close();
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("输入学号: ");
        String id = scanner.nextLine();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("输入电话: ");
        String phone = scanner.nextLine();
        Student newStudent = new Student(id, name, phone);
        students.add(newStudent);
        System.out.println("学生信息添加成功。");
    }

    private static void findStudent(Scanner scanner) {
        System.out.print("输入学号进行检索: ");
        String id = scanner.nextLine();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println(student);
                return;
            }
        }
        System.out.println("未找到该学生。");
    }

    private static void updateStudent(Scanner scanner) {
        System.out.print("输入学号进行更新: ");
        String id = scanner.nextLine();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.print("输入新姓名: ");
                String name = scanner.nextLine();
                System.out.print("输入新电话: ");
                String phone = scanner.nextLine();
                student = new Student(id, name, phone);
                System.out.println("学生信息更新成功。");
                return;
            }
        }
        System.out.println("未找到该学生。");
    }

    private static void deleteStudent(Scanner scanner) {
        System.out.print("输入学号进行删除: ");
        String id = scanner.nextLine();
        students.removeIf(student -> student.getId().equals(id));
        System.out.println("学生信息删除成功。");
    }

    private static void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载学生信息。");
        }
    }

    private static void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILENAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("无法保存学生信息。");
        }
    }
}